Sensor Fusion for Kinetis MCUs (ISSDK/KSDK version)
magnetic.h
Go to the documentation of this file.
1 // Copyright (c) 2014, 2015, 2016, NXP Semiconductors N.V.,
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of NXP Semiconductors N.V. nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL NXP SEMICONDUCTORS N.V. BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 /*! \file magnetic.h
27  \brief Lower level magnetic calibration interface
28 
29  Many developers can utilize the NXP Sensor Fusion Library without ever
30  making any adjustment to the lower level magnetic calibration functions
31  defined in this file.
32 */
33 
34 #ifndef MAGNETIC_H
35 #define MAGNETIC_H
36 
37 #ifndef F_USING_MAG
38 #define F_USING_MAG 0x0002 // normally should be defined in build.h
39 #endif
40 
41 #if F_USING_MAG
42 /// @name Magnetic Calibration Constants
43 ///@{
44 #define MAGBUFFSIZEX 14 ///< x dimension in magnetometer buffer (12x24 equals 288 elements)
45 #define MAGBUFFSIZEY (2 * MAGBUFFSIZEX) ///< y dimension in magnetometer buffer (12x24 equals 288 elements)
46 #define MINMEASUREMENTS4CAL 110 ///< minimum number of measurements for 4 element calibration
47 #define MINMEASUREMENTS7CAL 220 ///< minimum number of measurements for 7 element calibration
48 #define MINMEASUREMENTS10CAL 330 ///< minimum number of measurements for 10 element calibration
49 #define MAXMEASUREMENTS 360 ///< maximum number of measurements used for calibration
50 #define CAL_INTERVAL_SECS 300 ///< 300s or 5min interval for regular calibration checks
51 #define MINBFITUT 10.0F ///< minimum acceptable geomagnetic field B (uT) for valid calibration
52 #define MAXBFITUT 90.0F ///< maximum acceptable geomagnetic field B (uT) for valid calibration
53 #define FITERRORAGINGSECS 86400.0F ///< 24 hours: time (s) for fit error to increase (age) by e=2.718
54 #define MESHDELTACOUNTS 50 ///< magnetic buffer mesh spacing in counts (here 5uT)
55 #define DEFAULTB 50.0F ///< default geomagnetic field (uT)
56 ///@}
57 
58 /// The Magnetometer Measurement Buffer holds a 3-dimensional "constellation"
59 /// of data points.
60 ///
61 /// The constellation of points are used to compute magnetic hard/soft iron compensation terms.
62 /// The contents of this buffer are updated on a continuing basis.
63 typedef struct MagBuffer
64 {
65  int16_t iBs[3][MAGBUFFSIZEX][MAGBUFFSIZEY]; ///< uncalibrated magnetometer readings
66  int32_t index[MAGBUFFSIZEX][MAGBUFFSIZEY]; ///< array of time indices
67  int16_t tanarray[MAGBUFFSIZEX - 1]; ///< array of tangents of (100 * angle)
68  int16_t iMagBufferCount; ///< number of magnetometer readings
69 } MagBuffer;
70 
71 /// Magnetic Calibration Structure
72 typedef struct MagCalibration
73 {
74  // start of elements stored to flash memory on Save (16 * 4 = 64 bytes)
75  float fV[3]; ///< current hard iron offset x, y, z, (uT)
76  float finvW[3][3]; ///< current inverse soft iron matrix
77  float fB; ///< current geomagnetic field magnitude (uT)
78  float fBSq; ///< square of fB (uT^2)
79  float fFitErrorpc; ///< current fit error %
80  int32_t iValidMagCal; ///< solver used: 0 (no calibration) or 4, 7, 10 element
81  // end of elements stored to flash memory
82  // start of working elements not stored to flash memory
83  float ftrV[3]; ///< trial value of hard iron offset z, y, z (uT)
84  float ftrinvW[3][3]; ///< trial inverse soft iron matrix size
85  float ftrB; ///< trial value of geomagnetic field magnitude in uT
86  float ftrFitErrorpc; ///< trial value of fit error %
87  float fA[3][3]; ///< ellipsoid matrix A
88  float finvA[3][3]; ///< inverse of ellipsoid matrix A
89  float fmatA[10][10]; ///< scratch 10x10 float matrix used by calibration algorithms
90  float fmatB[10][10]; ///< scratch 10x10 float matrix used by calibration algorithms
91  float fvecA[10]; ///< scratch 10x1 vector used by calibration algorithms
92  float fvecB[4]; ///< scratch 4x1 vector used by calibration algorithms
93  float fYTY; ///< Y^T.Y for 4 element calibration = (iB^2)^2
94  int32_t iSumBs[3]; ///< sum of measurements in buffer (counts)
95  int32_t iMeanBs[3]; ///< average magnetic measurement (counts)
96  int32_t itimeslice; ///< counter for tine slicing magnetic calibration calculations
97  int8_t iCalInProgress; ///< flag denoting that a calibration is in progress
98  int8_t iNewCalibrationAvailable; ///< flag denoting that a new calibration has been computed
99  int8_t iInitiateMagCal; ///< flag to start a new magnetic calibration
100  int8_t iMagBufferReadOnly; ///< flag to denote that the magnetic measurement buffer is temporarily read only
101  int8_t i4ElementSolverTried; ///< flag to denote at least one attempt made with 4 element calibration
102  int8_t i7ElementSolverTried; ///< flag to denote at least one attempt made with 4 element calibration
103  int8_t i10ElementSolverTried; ///< flag to denote at least one attempt made with 4 element calibration
105 
106 
107 struct MagSensor; // actual typedef is located in sensor_fusion_types.h
108 
109 /// @name Function prototypes for functions in magnetic.c
110 /// These functions comprise the core of the magnetic calibration features of
111 /// the library. Parameter descriptions are not included here,
112 /// as details are provided in sensor_fusion.h.
113 ///@{
114 void fInitializeMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer);
115 void iUpdateMagBuffer(struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32_t loopcounter);
116 void fInvertMagCal(struct MagSensor *pthisMag, struct MagCalibration *pthisMagCal);
117 void fRunMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor* pthisMag, int32_t loopcounter);
118 void fUpdateMagCalibration4(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
119 void fUpdateMagCalibration7(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
120 void fUpdateMagCalibration10(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
121 void fUpdateMagCalibration4Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
122 void fUpdateMagCalibration7Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
123 void fUpdateMagCalibration10Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag);
124 ///@}
125 #else // if F_USING_MAG
126 typedef struct MagBuffer
127 {
128  void *placeholder;
129 } MagBuffer;
130 
131 /// Magnetic Calibration Structure
132 typedef struct MagCalibration
133 {
134  void *placeholder;
136 #endif // if F_USING_MAG
137 #endif // #ifndef MAGNETIC_H
float ftrB
trial value of geomagnetic field magnitude in uT
Definition: magnetic.h:85
int8_t i7ElementSolverTried
flag to denote at least one attempt made with 4 element calibration
Definition: magnetic.h:102
struct MagCalibration MagCalibration
Magnetic Calibration Structure.
#define MAGBUFFSIZEX
x dimension in magnetometer buffer (12x24 equals 288 elements)
Definition: magnetic.h:44
void fUpdateMagCalibration7Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:686
void fUpdateMagCalibration7(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
int16_t tanarray[MAGBUFFSIZEX-1]
array of tangents of (100 * angle)
Definition: magnetic.h:67
void fUpdateMagCalibration4Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:455
void fInitializeMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer)
Definition: magnetic.c:41
void fUpdateMagCalibration4(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
int8_t iNewCalibrationAvailable
flag denoting that a new calibration has been computed
Definition: magnetic.h:98
int8_t iInitiateMagCal
flag to start a new magnetic calibration
Definition: magnetic.h:99
int16_t iMagBufferCount
number of magnetometer readings
Definition: magnetic.h:68
Magnetic Calibration Structure.
Definition: magnetic.h:72
void fUpdateMagCalibration10Slice(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
Definition: magnetic.c:973
int8_t i4ElementSolverTried
flag to denote at least one attempt made with 4 element calibration
Definition: magnetic.h:101
int32_t iValidMagCal
solver used: 0 (no calibration) or 4, 7, 10 element
Definition: magnetic.h:80
float ftrFitErrorpc
trial value of fit error %
Definition: magnetic.h:86
int8_t iMagBufferReadOnly
flag to denote that the magnetic measurement buffer is temporarily read only
Definition: magnetic.h:100
struct MagBuffer MagBuffer
The Magnetometer Measurement Buffer holds a 3-dimensional "constellation" of data points...
float fFitErrorpc
current fit error %
Definition: magnetic.h:79
int8_t iCalInProgress
flag denoting that a calibration is in progress
Definition: magnetic.h:97
float fYTY
Y^T.Y for 4 element calibration = (iB^2)^2.
Definition: magnetic.h:93
void fInvertMagCal(struct MagSensor *pthisMag, struct MagCalibration *pthisMagCal)
Definition: magnetic.c:297
void fRunMagCalibration(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32_t loopcounter)
Definition: magnetic.c:325
int32_t index[MAGBUFFSIZEX][MAGBUFFSIZEY]
array of time indices
Definition: magnetic.h:66
The MagSensor structure stores raw and processed measurements for a 3-axis magnetic sensor...
float fB
current geomagnetic field magnitude (uT)
Definition: magnetic.h:77
The Magnetometer Measurement Buffer holds a 3-dimensional "constellation" of data points...
Definition: magnetic.h:63
float fBSq
square of fB (uT^2)
Definition: magnetic.h:78
#define MAGBUFFSIZEY
y dimension in magnetometer buffer (12x24 equals 288 elements)
Definition: magnetic.h:45
void fUpdateMagCalibration10(struct MagCalibration *pthisMagCal, struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag)
void iUpdateMagBuffer(struct MagBuffer *pthisMagBuffer, struct MagSensor *pthisMag, int32_t loopcounter)
Definition: magnetic.c:103
int16_t iBs[3][MAGBUFFSIZEX][MAGBUFFSIZEY]
uncalibrated magnetometer readings
Definition: magnetic.h:65
int32_t itimeslice
counter for tine slicing magnetic calibration calculations
Definition: magnetic.h:96
int8_t i10ElementSolverTried
flag to denote at least one attempt made with 4 element calibration
Definition: magnetic.h:103